package com.azxc.rapid.plus.user.controller;

import com.azxc.rapid.core.cache.utils.CacheUtil;
import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.mp.support.Query;
import com.azxc.rapid.core.secure.RapidUser;
import com.azxc.rapid.core.tenant.annotation.NonDS;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.constant.RapidConstant;
import com.azxc.rapid.core.tool.utils.StringPool;
import com.azxc.rapid.plus.user.service.ICommonUserService;
import com.azxc.rapid.system.entity.Tenant;
import com.azxc.rapid.system.feign.ISysClient;
import com.azxc.rapid.system.service.ITenantService;
import com.azxc.rapid.system.user.entity.User;
import com.azxc.rapid.system.user.service.IUserService;
import com.azxc.rapid.system.user.vo.UserVO;
import com.azxc.rapid.system.user.wrapper.UserWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

import static com.azxc.rapid.core.cache.constant.CacheConstant.USER_CACHE;

/**
 * 控制器
 * @author 91311
 */
@NonDS
@RestController
@RequestMapping("/common")
@AllArgsConstructor
@Api(value = "通用-用户信息")
public class CommonUserController {

	private final IUserService userService;

	private final ICommonUserService commonUserService;

	private final ISysClient iSysClient;


	/**
	 * 查询单条
	 */
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "查看详情", notes = "传入id")
	@GetMapping("/user/detail")
	public R<UserVO> detail(User user) {
		User detail = userService.getOne(Condition.getQueryWrapper(user));
		return R.data(UserWrapper.build().entityVO(detail));
	}



	/**
	 * 自定义用户列表
	 */
	@GetMapping("/user/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	public R<IPage<UserVO>> page(@ApiIgnore User user, Query query, Long deptId, RapidUser rapidUser) {
		IPage<User> pages = commonUserService.selectUserPage(Condition.getPage(query), user, deptId, (rapidUser.getTenantId().equals(RapidConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : rapidUser.getTenantId()));
		return R.data(UserWrapper.build().pageVO(pages));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/user/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入User")
	public R submit(@Valid @RequestBody User user) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.submit(user));
	}


	/**
	 * 删除
	 */
	@PostMapping("/user/remove")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "删除", notes = "传入id集合")
	public R remove(@RequestParam String ids) {
		CacheUtil.clear(USER_CACHE);
		return R.status(userService.removeUser(ids));
	}

	private final ITenantService tenantService;

	@GetMapping("/tenant/select")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "下拉租户", notes = "传入tenant")
	public R<List<Tenant>> select(Tenant tenant, RapidUser rapidUser) {
		QueryWrapper<Tenant> queryWrapper = Condition.getQueryWrapper(tenant);
		List<Tenant> list = tenantService.list((!rapidUser.getTenantId().equals(RapidConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Tenant::getTenantId, rapidUser.getTenantId()) : queryWrapper);
		return R.data(list);
	}

}
